<?php
namespace My\Controller;

use Utuo\BackgroundController;

class DistributionShopUserWithdrawController extends BackgroundController {
	
	public $status = array(
		'0' => '未付款',
		'1' => '已付款'
	);
	
	public function _initialize() {
		$this->setControllerTitle("用户提现申请");
		$this->setActionTitle(array(
				'index' => '申请列表',
				'details' => '提现详细',
		));
		$this->setActionNav("index");
		foreach ($this->status as $k => $v) {
			$this->setLinkNav($v, U("DistributionShopUserWithdraw/index", "status={$k}"));
		}
	}
	
	public function getLists( $is_deleted ) {
		$status = I("status", -1, 'intval');
		$page = I('p', 0, 'intval');
		$condition = array (
				'a.cid' => $this->aid,
		);
		$status !== -1 and $condition['status'] = $status;
		$m = M("DistributionShopUserWithdraw a")->join("__WECHAT_USER__ b on a.uid=b.id", "LEFT")->where($condition)->field("a.*,b.nickname,b.face,b.openId");
		$n = clone $m;
		$driverCount = $n->count();
		$distributionShopUserWithdrawLists = $m->page($page)->limit($this->getPageSize())->order("a.createTime desc,a.id asc")->select();
		foreach ( $distributionShopUserWithdrawLists as &$v ) {
			$v['statusName'] = $this->status[$v['status']];
		}
		$this->showPage($driverCount, $this->getPageSize());
		$this->assign("distributionShopUserWithdrawLists", $distributionShopUserWithdrawLists);
		$this->display();
	}	
	
	public function detailsAction() {
		$id = I('id', 0, 'intval');
		$condition = array (
			'a.cid' => $this->aid,
			'a.id' => $id,
		);
		$m = M("DistributionShopUserWithdraw a")->join("__WECHAT_USER__ b on a.uid=b.id", "LEFT")->where($condition)->field("a.*,b.nickname,b.face,b.openId");
		$distributionShopUserWithdraw = $m->find();
		if ( $distributionShopUserWithdraw ) {
			$distributionShopUserWithdraw['showStatusName'] = $this->status[$distributionShopUserWithdraw['status']];;
		}
		$this->assign("distributionShopUserWithdraw", $distributionShopUserWithdraw);
		$this->display();
	}
	
	/**
	 * 确认已打款
	 */
	public function confirmPostAction() {
		$id = I('id', 0, "intval");
		$withdraw = M("DistributionShopUserWithdraw")->where(array('id'=>$id, 'cid'=> $this->aid))->find();
		if ( $withdraw ) {
			if ( $withdraw['status'] == 0 ) {
				if ( false !== M("DistributionShopUserWithdraw")->where(array('id'=>$id, 'cid'=> $this->aid))
								->save(array(
									'payTime' => time(),
									'status' => 1,
								)) ) {
								if ( false !== M("WechatUser")->where("id=%d and cid=%d", $withdraw['uid'], $this->aid)->setInc('mention', $withdraw['money']) ) {
									$this->ajaxError("申请已处理完成");
								}
				}
				$this->ajaxError("申请处理保存失败");
			} else {
				$this->ajaxError("此提现申请已处理过了");
			}
		}
	}
}